<template>
  <div class="brands-page">
    <h1 class="title">品牌</h1>
    <el-card class="content-card">
      <div class="brand-grid" v-loading="loading">
        <div v-for="b in brands" :key="b.brandId" class="brand-item" @click="goBrand(b.brandId)">
          <div class="logo-wrap">
            <img :src="b.brandLogo || placeholder" :alt="b.brandName" />
          </div>
          <div class="info">
            <h3>{{ b.brandName }}</h3>
            <p v-if="b.productCount != null">商品数：{{ b.productCount }}</p>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { productService } from '@/api'

type BrandRow = {
  brandId: number
  brandName: string
  brandLogo?: string
  productCount?: number
}

const router = useRouter()
const loading = ref(false)
const brands = ref<BrandRow[]>([])
const placeholder = '/placeholder/brand.png'

const loadBrands = async () => {
  loading.value = true
  try {
    const res = await productService.getProductBrands()
    const raw = (res.data as any)?.data || []
    const baseUrl = 'http://localhost:8083'
    brands.value = (raw as any[]).map((b: any) => {
      const logoPath = b.brandLogo ?? b.logo ?? b.brand_logo ?? ''
      return {
        brandId: b.brand_id ?? b.brandId ?? b.id,
        brandName: b.brand_name ?? b.brandName ?? b.name,
        brandLogo: logoPath && logoPath.startsWith('/') ? baseUrl + logoPath : logoPath,
        productCount: b.productCount ?? b.products ?? undefined,
      }
    })
  } finally {
    loading.value = false
  }
}

const goBrand = (id: number | undefined | null) => {
  if (typeof id !== 'number' || Number.isNaN(id)) return
  router.push({ name: 'BrandProducts', params: { id } })
}

onMounted(loadBrands)
</script>

<style scoped>
.brands-page {
  padding: 20px;
}
.title {
  margin: 0 0 12px 0;
  font-size: 22px;
}
.content-card {
  border: none;
}
.brand-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 16px;
  max-width: 100%;
  overflow: hidden;
}
.brand-item {
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 16px;
  border: 1px solid #eee;
  border-radius: 8px;
  background: #fff;
  transition:
    box-shadow 0.2s ease,
    transform 0.2s ease;
  min-width: 0; /* 防止内容撑大 */
  width: 100%;
}
.brand-item:hover {
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
  transform: translateY(-2px);
}
.logo-wrap {
  width: 100%;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.logo-wrap img {
  max-width: 100%;
  max-height: 80px;
  object-fit: contain;
}
.info {
  text-align: center;
  width: 100%;
}
.info h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}
.info p {
  margin: 6px 0 0 0;
  color: #999;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .brands-page {
    padding: 15px;
  }
  
  .title {
    font-size: 20px;
  }
  
  .brand-grid {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
  }
  
  .brand-item {
    padding: 12px;
    
    .logo-wrap {
      height: 60px;
    }
    
    .info h3 {
      font-size: 14px;
    }
    
    .info p {
      font-size: 11px;
    }
  }
}

@media (max-width: 480px) {
  .brands-page {
    padding: 10px;
  }
  
  .title {
    font-size: 18px;
  }
  
  .brand-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }
  
  .brand-item {
    padding: 10px;
    
    .logo-wrap {
      height: 50px;
    }
    
    .info h3 {
      font-size: 13px;
    }
    
    .info p {
      font-size: 10px;
    }
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  .brand-grid {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 18px;
  }
  
  .brand-item {
    padding: 18px;
    
    .logo-wrap {
      height: 90px;
    }
    
    .info h3 {
      font-size: 17px;
    }
    
    .info p {
      font-size: 13px;
    }
  }
}

@media (min-width: 1025px) {
  .brand-grid {
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    max-width: 1200px;
    margin: 0 auto;
  }
  
  .brand-item {
    padding: 20px;
    
    .logo-wrap {
      height: 100px;
    }
    
    .info h3 {
      font-size: 18px;
    }
    
    .info p {
      font-size: 14px;
    }
  }
}

@media (min-width: 1400px) {
  .brand-grid {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 24px;
    max-width: 1400px;
  }
  
  .brand-item {
    padding: 24px;
    
    .logo-wrap {
      height: 110px;
    }
    
    .info h3 {
      font-size: 19px;
    }
    
    .info p {
      font-size: 15px;
    }
  }
}
</style>
